<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
// require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '700px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.chart.resize()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions({ xData, totalPowerConsumption ,dailyPowerConsumption ,dailyElectricityUsage ,dailyChargingCapacity} = {}) {
      this.chart.clear();
      if(!xData||xData.length<=0){
        this.chart.setOption({
          title: {
          text: '暂无数据',
          x: 'center',
          y: 'center',
          textStyle: {
            fontSize: 30,
            color: '#909399',
            fontWeight: 'normal'
          }
        }
        })
      }else{
        this.chart.setOption({
          color:['#4B68B4','#029BA4','#FBC44D','#FD6527'],
          xAxis: {
            data: xData,
            boundaryGap: false,
            axisTick: {
              show: false
            }
          },
          title:{
            show:false
          },
          grid: {
            left: 50,
            right: 60,
            bottom: 50,
            top: 50,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            name:'kWh',
            axisTick: {
              show: false
            }
          },
          legend: {
            show:true,
            position:"top",
            data:['总用电量',"直流用电量","市电用电量",'总充电量']
          },
          series: [ {
            name:"总用电量",
            type: 'line',
            data: totalPowerConsumption,
            smooth: true,
            symbolSize:10,
            label:{
              show:true,
              color:'#666',
              position:'bottom',
              fontSize:14,
              textBorderColor:'#fff',
              textBorderWidth:2,
              textShadowColor:'#fff',
            },
            animation:true,
            lineStyle:{
              width:3
            },
            markPoint: {
              data: [
                { type: 'max', name: 'Max'},
                { type: 'min', name: 'Min'}
              ]
            },
          },
          {
            name:"直流用电量",
            type: 'line',
            data: dailyPowerConsumption,
            smooth: true,
            symbolSize:10,
            label:{
              show:true,
              color:'#666',
              position:'bottom',
              fontSize:14,
              textBorderColor:'#fff',
              textBorderWidth:2,
              textShadowColor:'#fff',
            },
            animation:true,
            lineStyle:{
              width:3
            },
            markPoint: {
              data: [
                { type: 'max', name: 'Max'},
                { type: 'min', name: 'Min'}
              ]
            },
          },
          {
            name:"市电用电量",
            type: 'line',
            data: dailyElectricityUsage,
            smooth: true,
            symbolSize:10,
            label:{
              show:true,
              color:'#666',
              position:'bottom',
              fontSize:14,
              textBorderColor:'#fff',
              textBorderWidth:2,
              textShadowColor:'#fff',
            },
            animation:true,
            lineStyle:{
              width:3
            },
            markPoint: {
              data: [
                { type: 'max', name: 'Max'},
                { type: 'min', name: 'Min'}
              ]
            },
          },
          ,
          {
            name:'总充电量',
            type: 'line',
            data: dailyChargingCapacity,
            smooth: true,
            symbolSize:10,
            label:{
              show:true,
              color:'#666',
              position:'bottom',
              fontSize:14,
              textBorderColor:'#fff',
              textBorderWidth:2,
              textShadowColor:'#fff',
            },
            animation:true,
            lineStyle:{
              width:3
            },
            markPoint: {
              data: [
                { type: 'max', name: 'Max'},
                { type: 'min', name: 'Min'}
              ]
            },
          }
        ],
          dataZoom: [
            {
              type: 'slider',
              xAxisIndex: 0,
              filterMode:'filter',
              dataBackground:{
                areaStyle:{
                  color:'#03C2C2'
                }
              }
            }
          ],
          animation:true,
          animationDuration: 1500,
          animationEasing: 'cubicInOut',
          animationEasingUpdate: 'cubicInOut'
        })
      }

    }
  }
}
</script>
